package com.bawei.wyg.mvvmcommon.mvvm.viewmodel;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.bawei.wyg.mvvmcommon.fieid.NetWorkFieid;
import com.bawei.wyg.mvvmcommon.fieid.UiField;
import com.bawei.wyg.mvvmcommon.mvvm.model.IModel;
import com.bawei.wyg.networkmodel.entity.BaseEntity;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.NetworkUtils;

import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

public abstract class BaseViewModel<M extends IModel> extends ViewModel implements IViewModel, Observer<BaseEntity> {

    protected M mModel;
    private CompositeDisposable compositeDisposable;
    private UIChangeLiveData uiChangeLiveData;

    public BaseViewModel() {

    }


    @Override
    public void onSubscribe(@io.reactivex.annotations.NonNull Disposable d) {
        compositeDisposable.add(d);
        uiChangeLiveData.setShowDialog(UiField.SHOWDIALOG+System.currentTimeMillis());
    }

    @Override
    public void onNext(@io.reactivex.annotations.NonNull BaseEntity baseEntity) {
        if(baseEntity.getStatus()== NetWorkFieid.NETWORKERRO){
            LogUtils.e("无网络连接");
            return;
        }
        sucessFull(baseEntity);
    }

    @Override
    public void onError(@io.reactivex.annotations.NonNull Throwable e) {
        LogUtils.e(e.getMessage());
    }

    @Override
    public void onComplete() {
        uiChangeLiveData.setHideDialog(UiField.SHOWDIALOG+System.currentTimeMillis());

    }

    @Override
    public void onCreate() {
        compositeDisposable=new CompositeDisposable();
        mModel=createModel();
        requestAll();
    }

    protected void requestAll(){
        if(NetworkUtils.isConnected()){
            initData();
        }else{
            BaseEntity baseEntity=new BaseEntity();
            baseEntity.setStatus(NetWorkFieid.NETWORKERRO);
            baseEntity.setMessage("网络连接异常，请检查网络");
            Observable observable=Observable.just(baseEntity);
            observable.subscribe(this);
        }
    }

    protected abstract void initData();

    protected abstract M createModel();

    protected abstract void sucessFull(BaseEntity baseEntity);


    @Override
    public void onDestory() {
        if(compositeDisposable!=null){
            compositeDisposable.dispose();
        }
    }

    public UIChangeLiveData getUiChangeLiveData(){
        if(uiChangeLiveData==null){
            uiChangeLiveData=new UIChangeLiveData();
        }
        return uiChangeLiveData;
    }

    public class UIChangeLiveData{
        MutableLiveData<String> showDialog=new MutableLiveData<>();
        MutableLiveData<String> hideDialog=new MutableLiveData<>();
        MutableLiveData<String> finishActivity=new MutableLiveData<>();
        MutableLiveData<Map<String,Object>> startActivity=new MutableLiveData<>();

        public MutableLiveData<String> getShowDialog() {
            return showDialog;
        }

        public void setShowDialog(String showDialogStr) {
            this.showDialog.setValue(showDialogStr);
        }

        public MutableLiveData<String> getHideDialog() {
            return hideDialog;
        }

        public void setHideDialog(String hideDialogStr) {
            this.hideDialog.setValue(hideDialogStr);
        }

        public MutableLiveData<String> getFinishActivity() {
            return finishActivity;
        }

        public void setFinishActivity(String finishActivityStr) {
            this.finishActivity.setValue(finishActivityStr);
        }

        public MutableLiveData<Map<String, Object>> getStartActivity() {
            return startActivity;
        }

        public void setStartActivity(Map<String, Object> startActivityMap) {
            this.startActivity.setValue(startActivityMap);
        }
    }
}
